<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width,initial-scale=1 user-scalable=0"
    />
    <title>transform居中</title>
    <style>
      html,
      body,
      section {
        padding: 0;
        margin: 0;
        width: 100%;
        height: 100%;
        position: relative;
      }

      a {
        color: #39c;
        text-decoration: none;
        display: inline-block;
      }

      header {
        top: 0;
        position: fixed;
        padding: 0.5rem 1rem;
        background: #def;
        width: 100%;
        display: block;
        z-index: 1;
      }

      header a {
        display: inline-block;
        padding: 0.5rem;
      }
      header a:hover {
        background: #39c;
        color: #fff;
      }

      .divTransform {
        position: absolute;
        top: 50%;
        left: 50%;
        -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
        -o-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
      }

      .bgBlue {
        background: #def;
        padding: 1rem;
      }
    </style>
  </head>
  <body>
    <header>
      <div>
        <a href="index.html">transform方式</a>
        <a href="margin.html">margin负值方式</a>
        <a href="margin-auto.html">margin-auto方式</a>
        <a href="table.html">display table方式</a>
        <a href="flex.html">flex方式</a>
      </div>
    </header>
    <section>
      <div class="bgBlue divTransform">
        使用transform实现页面垂直居中,因为transform是css3属性，在所有的移动端都可以正常显示。IE9+也可以正常显示。传统中margin为负的方法，在不知道浮动对象高宽的方式下，就不好居中了。
        <a
          href="https://github.com/cosyer/positionCenter/blob/master/index.html"
          >查看源代码</a
        >
      </div>
    </section>
  </body>
</html>
